package com.lineage.list;

import com.lineage.server.utils.StreamUtil;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.StringTokenizer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class BadNamesList {
  private static final Log _log = LogFactory.getLog(BadNamesList.class);
  
  private static BadNamesList _instance;
  
  private ArrayList<String> _nameList = new ArrayList<>();
  
  public static BadNamesList get() {
    if (_instance == null)
      _instance = new BadNamesList(); 
    return _instance;
  }
  
  public void load() {
    LineNumberReader lnr = null;
    try {
      File file = new File("data/badnames.txt");
      FileInputStream fileIn = new FileInputStream(file);
      InputStreamReader inputStream = new InputStreamReader(fileIn, "utf8");
      lnr = new LineNumberReader(inputStream);
      boolean isWhile = false;
      String line = null;
      while ((line = lnr.readLine()) != null) {
        if (!isWhile) {
          isWhile = true;
          continue;
        } 
        if (line.trim().length() == 0)
          continue; 
        if (line.startsWith("#"))
          continue; 
        StringTokenizer st = new StringTokenizer(line, ";");
        String ban = st.nextToken();
        this._nameList.add(ban);
      } 
      _log.info("载入禁止名称数量: " + this._nameList.size());
    } catch (FileNotFoundException e) {
      _log.error(e.getLocalizedMessage(), e);
    } catch (Exception e2) {
      _log.error(e2.getLocalizedMessage(), e2);
    } finally {
      StreamUtil.close(new Closeable[] { lnr });
    } 
  }
  
  public boolean isBadName(String name) {
    String checkName = name.toLowerCase();
    Iterator<String> iterator = this._nameList.iterator();
    while (iterator.hasNext()) {
      String badName = iterator.next();
      if (checkName.indexOf(badName.toLowerCase()) != -1) {
        _log.info("新建人物名称包含禁用字元: " + this._nameList.size());
        return true;
      } 
    } 
    return false;
  }
  
  public String[] getAllBadNames() {
    return this._nameList.<String>toArray(new String[this._nameList.size()]);
  }
}
